package com.qi.services.impl;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qi.common.ApplyStatus;
import com.qi.common.RoleKey;
import com.qi.exception.ServerException;
import com.qi.mapper.AppProcessMapper;
import com.qi.pojo.dto.ApplyClubBody;
import com.qi.pojo.entity.AppProcess;
import com.qi.pojo.entity.Club;
import com.qi.pojo.entity.User;
import com.qi.pojo.vo.UserDetails;
import com.qi.services.IAppProcessService;
import com.qi.services.IClubService;
import com.qi.services.IUserService;
import com.qi.utils.MailUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AppProcessServiceImpl extends ServiceImpl<AppProcessMapper, AppProcess> implements IAppProcessService {

    @Resource
    private IUserService userService;

    @Resource
    private MailUtil mailUtil;

    @Resource
    private IClubService clubService;

    @Override
    public void apply(ApplyClubBody applyClubBody) {
        UserDetails curUser = userService.getCurUser();
        // 如果已经申请过了，并且处于等待审核状态，则无法继续申请
        LambdaQueryWrapper<AppProcess> appProcessQueryWrapper = new LambdaQueryWrapper<AppProcess>()
                .eq(AppProcess::getUserId, curUser.getId())
                .eq(AppProcess::getState, ApplyStatus.待审核);
        AppProcess process = this.getOne(appProcessQueryWrapper);
        if (ObjUtil.isNotEmpty(process)) {
            throw new ServerException("不可重复申请，请耐心等待社长审核");
        }
        AppProcess appProcess = new AppProcess();
        appProcess.setName(curUser.getName());
        appProcess.setContent(applyClubBody.getContent());
        appProcess.setState(ApplyStatus.待审核);
        appProcess.setUserId(curUser.getId());
        appProcess.setClubId(applyClubBody.getClubId());

        // 持久化
        this.save(appProcess);
    }

    @Override
    public IPage<AppProcess> getList(Long current, Long size) {
        User president = userService.getCurUserInfo();
        IPage<AppProcess> appProcessPage = new Page<>(current, size);
        LambdaQueryWrapper<AppProcess> appProcessLambdaQueryWrapper = new LambdaQueryWrapper<AppProcess>()
                .eq(AppProcess::getClubId, president.getClubId());
        appProcessPage = this.page(appProcessPage, appProcessLambdaQueryWrapper);
        return appProcessPage;
    }

    @Override
    public IPage<AppProcess> getPendingApprovalList(Long current, Long size) {
        User president = userService.getCurUserInfo();
        IPage<AppProcess> appProcessPage = new Page<>(current, size);
        LambdaQueryWrapper<AppProcess> appProcessLambdaQueryWrapper = new LambdaQueryWrapper<AppProcess>()
                .eq(AppProcess::getClubId, president.getClubId())
                .eq(AppProcess::getState, ApplyStatus.待审核);
        appProcessPage = this.page(appProcessPage, appProcessLambdaQueryWrapper);
        return appProcessPage;
    }

    @Override
    public void approval(Long id, Long userId, Long clubId) {
        // 获取审核人信息
        User auditor = userService.getCurUserInfo();
        // 修改申请记录
        LambdaUpdateWrapper<AppProcess> appProcessUpdateWrapper = new LambdaUpdateWrapper<AppProcess>()
                .set(AppProcess::getAuditor, auditor.getName())
                .set(AppProcess::getAuditTime, new Date())
                .set(AppProcess::getState, ApplyStatus.批准)
                .eq(AppProcess::getId, id);
        this.update(appProcessUpdateWrapper);
        // 修改用户信息
        userService.setRole(userId, RoleKey.社员);
        LambdaUpdateWrapper<User> userUpdateWrapper = new LambdaUpdateWrapper<User>()
                .set(User::getClubId, clubId)
                .eq(User::getId, userId);
        userService.update(userUpdateWrapper);
        // 发送通知消息
        User user = userService.getById(userId);
        Club club = clubService.getById(clubId);
        mailUtil.sendEmailApproval(user.getEmail(), user.getName(), club.getName());
    }

    @Override
    public void reject(Long id, Long userId, Long clubId) {
        // 获取审核人信息
        User auditor = userService.getCurUserInfo();
        // 修改申请记录
        LambdaUpdateWrapper<AppProcess> appProcessUpdateWrapper = new LambdaUpdateWrapper<AppProcess>()
                .set(AppProcess::getAuditor, auditor.getName())
                .set(AppProcess::getAuditTime, new Date())
                .set(AppProcess::getState, ApplyStatus.拒绝)
                .eq(AppProcess::getId, id);
        this.update(appProcessUpdateWrapper);
        // 发送通知消息
        User user = userService.getById(userId);
        Club club = clubService.getById(clubId);
        mailUtil.sendEmailReject(user.getEmail(), user.getName(), club.getName());
    }
}
